package com.it.gw.modules.user.fragment

import LoginVo
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.viewModels
import com.it.common.utils.ActivityUtils
import com.it.common.utils.DialogUtils
import com.it.data.manager.AppCacheManager
import com.it.data.manager.AppManager
import com.it.gw.R
import com.it.gw.base.BaseFragment
import com.it.gw.databinding.FragmentMyBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.user.activity.AboutActivity
import com.it.gw.modules.user.activity.LoginActivity
import com.it.gw.modules.user.activity.QcodActivity
import com.it.gw.modules.user.viewmodel.LoginViewModel
import com.tencent.mmkv.MMKV


/**
 * 我的
 */
class MyFragment : BaseFragment<FragmentMyBinding>() {
    private val loginViewModel by viewModels<LoginViewModel>()

    companion object {
        const val TAG = "MyFragment"
    }

    override fun initObserver() {

    }


    override fun onResume() {
        super.onResume()
    }

    override fun initData() {

    }

    override fun initView() {
        binding.tvVersion.text = "当前版本${AppManager.getAppVersionName(requireActivity())}"
        val loginVo = MMKV.defaultMMKV()
            .decodeParcelable(AppCacheManager.KEY_LOGIN_DATA, LoginVo::class.java)
        Log.d(TAG, "initView: ${loginVo}")
        binding.username.text = "${loginVo?.userName}"

    }

    override fun initListener() {
        binding.rlGwHot.setOnClickListener {
            Toast.makeText(requireActivity(),"暂未开放",Toast.LENGTH_SHORT).show()
        }
        binding.rlLogout.setOnClickListener {
            tryToLogout()

        }

        binding.rlAbout.setOnClickListener {
            startActivity(Intent(requireActivity(),AboutActivity::class.java))
        }
        binding.rlCheckVersion.setOnClickListener {
            startActivity(Intent(requireActivity(),QcodActivity::class.java))
        }

    }

    private fun tryToLogout() {
        val pwdDialog =
            DialogUtils.getDialogNotTouchOutside(requireActivity(), R.layout.dialog_common_tips)
        pwdDialog.show()
        val tip = pwdDialog.findViewById<TextView>(R.id.tip)
        val content = pwdDialog.findViewById<TextView>(R.id.content)
        tip.text = "提示"
        content.text = "是否确认退出登录？"
        val tvOK = pwdDialog.findViewById<TextView>(R.id.tv_ok)
        val tvCancel = pwdDialog.findViewById<TextView>(R.id.tv_cancel)

        tvOK.setOnClickListener {
            pwdDialog.dismiss()
            ProgressDialog.show(requireActivity())
            loginViewModel.logout()
            ProgressDialog.hide()
            ActivityUtils.finishAll()
            startActivity(Intent(requireActivity(), LoginActivity::class.java))
        }
        tvCancel.setOnClickListener {
            pwdDialog.dismiss()
        }


    }


    override fun getView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentMyBinding.inflate(layoutInflater, container, false)
        return binding.root
    }
}